package com.r2coding.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

/**
 * @author:98k灬
 * @since:2022/05/09 12:14
 **/
@Slf4j
public class HttpUtil {
    // 远程服务的接口接口url
    private static final String URL
            = "https://server01.vicy.cn/8lXdSX7FSMykbl9nFDWESdc6zfouSAEz/wxLogin/tempUserId?secretKey=779ed55680f54189ab8b1fae0c515d75";
    // 请求的content_type
    private static final String CONTENT_TYPE = "application/json";

    /**
     * 调用远程服务接口
     *
     * @return 远程服务接口返回的数据字符串
     */
    public static String doGet() {
        String result = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            HttpGet httpGet = new HttpGet(URL);
            httpGet.setHeader("Content-type", CONTENT_TYPE);
            result = httpClient.execute(httpGet, responseHandler);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
        return result ;
    }
}
